
// Shorthand
if (typeof(Cc) == "undefined")
  var Cc = Components.classes;
if (typeof(Ci) == "undefined")
  var Ci = Components.interfaces;
if (typeof(Cu) == "undefined")
  var Cu = Components.utils;
if (typeof(Cr) == "undefined")
  var Cr = Components.results;

Cu.import("resource://app/jsmodules/SBJobUtils.jsm");
Cu.import("resource://app/jsmodules/SBTimer.jsm");

const ALBUMVIEW_PREF_FIRSTRUN = "songbird.extensions.albumview.firstrun";

// Make a namespace.
if (typeof AlbumView == 'undefined') {
  var AlbumView = {};
}

// Called when the window finishes loading
AlbumView.onLoad = function() {
  window.removeEventListener("load", function(e) { AlbumView.onLoad(e); }, false);
  
  function scanLibrary() {
    // Load up the sbAlbumService and scan for albums
    var albumService = Cc["@songbirdnest.com/Songbird/album-service;1"]
                         .createInstance(Ci.sbIAlbumService);
    var albumViewScanJob = albumService.scanMainLibraryForAlbums();
    // Set up a job to watch
    SBJobUtils.showProgressDialog(albumViewScanJob, window, 0);
  }
  
  if (Application.prefs.getValue(ALBUMVIEW_PREF_FIRSTRUN, true)) {
    Application.prefs.setValue(ALBUMVIEW_PREF_FIRSTRUN, false);
    this._timer = new SBTimer(scanLibrary, 500, Ci.nsITimer.TYPE_ONE_SHOT);
  }
}

// Called when the window fis about to close
AlbumView.onUnload = function() {
  window.removeEventListener("unload", function(e) { AlbumView.onUnload(e); }, false);
  if (this._timer) {
    this._timer.cancel();
  }
}

window.addEventListener("load", function(e) { AlbumView.onLoad(e); }, false);
window.addEventListener("unload", function(e) { AlbumView.onUnload(e); }, false);
